home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 10468 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  72 lines

  1. Newsgroups: comp.lang.c++
  2. Path: night.primate.wisc.edu!relay!news
  3. From: Tom McLintock <tmclint>
  4. Subject: An (Ada-style) enum template class??
  5. Content-Type: text/plain; charset=us-ascii
  6. Message-ID: <1996Mar8.032801.27864@relay.nswc.navy.mil>
  7. Sender: news@relay.nswc.navy.mil
  8. Content-Transfer-Encoding: 7bit
  9. Organization: Naval Surface Warfare Center - Dahlgren Div.
  10. Mime-Version: 1.0
  11. Date: Fri, 8 Mar 1996 03:28:01 GMT
  12. X-Mailer: Mozilla 1.12IS (X11; I; IRIX 5.3 IP22)
  13. X-Url: news:comp.lang.c++
  14.  
  15. Has anyone made a general-purpose, powerful enum "container" class?  This would
  16. create a class containing full functionality for an enum you give it, or a list
  17. of enum values you give it.  With a variable of such a "type", you could 
  18.  
  19. - read an enum value from a text file into the variable and write its value to
  20.   the screen (not just its numeric int-value, but a string representation of
  21. the
  22.   value's name)
  23.  
  24. - increment and decrement the variable with ++ and --, and throw an exception 
  25.   when you go off one end of the enumerated values
  26.  
  27. - get the number of enum values so you can, say, define an array indexed by 
  28.   the enum class
  29.  
  30. - add values to the enum class when deriving a new class from the base enum
  31.   class
  32.  
  33. - of course use the variable as an lvalue or an rvalue that works just like a
  34.   normal enum variable
  35.  
  36. - all this without having to specify the values of the enum to the class as 
  37.   both an actual enum type and an array of string representations of the enum
  38.   values.
  39.  
  40. I've come up with solutions to some of these problems using, among other
  41. tricks,
  42. BIG #define macros and bizarre non-C/C++-looking syntax, but there MUST be a
  43. better way!  All of the above features are useful and are in Ada (a language
  44. I'm
  45. not generally fond of otherwise).  Bjarne Stroustrup seems to think that an
  46. enum
  47. shouldn't have to do all this, from what I've read, but my experiences with Ada
  48. suggest otherwise.
  49.  
  50. BTW, I've found that the humble #define still has a lot of usefulness in C++,
  51. in
  52. spite of what many writers claim.  For example, I wrote a trace macro that
  53. lets you write, for example,
  54. TRACE(i _ x _ str); // i, x, and str are int, float and char* variables
  55. and when the program runs, the output from the TRACE call is something like
  56. ###filename.C (line 234)###
  57.     i: 42
  58.     x: 3.14159
  59.     str: "Hello world"
  60. Let's see anyone do that in C or without using the preprocessor!
  61.  
  62. -- 
  63.   - Tom McLintock
  64.    .                // 
  65. ___|\____|_________//______________/|______
  66. ___|/_|__|)_______//|_________|\__/_|______
  67. __/|__|)____|____//_|___/\____|_\/__|______
  68. _{_|\_______|)______|___\/____|_____|______
  69. __\|/_____________(@)_________|___(@)______
  70.    |                        (@)
  71.  
  72.